package com.newviews.tcmip.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newviews.tcmip.common.enums.UiElCodeEnums;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.pojo.po.*;
import com.newviews.tcmip.dao.EtcmPxTreatiseMapper;
import com.newviews.tcmip.service.inf.EtcmPxTreatiseServiceInf;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 处方-文献关系表 服务实现类
 * </p>
 *
 * @author lxc
 * @since 2024-03-08
 */
@Service
public class EtcmPxTreatiseServiceImpl extends ServiceImpl<EtcmPxTreatiseMapper, EtcmPxTreatisePo> implements EtcmPxTreatiseServiceInf {
    @Resource
    private EtcmPxTreatiseMapper etcmPxTreatiseMapper;
    @Override
    public ResultModel<PageModel<Map<String, String>>> getPage(String treatiseType, String uiTypeCode, String langCode, Integer pageNo, Integer pageSize) {
        IPage<EtcmTreatiseExPo> iPage = etcmPxTreatiseMapper.getPage(new Page<>(pageNo, pageSize),treatiseType,uiTypeCode,langCode);
        List<Map<String,String>> list=new ArrayList<>();
        for (EtcmTreatiseExPo record : iPage.getRecords()) {
            Map<String,String> map=new HashMap<>(16);
            String[] uiElCodeList = record.getUiElCode().split("\\|\\|");
            String[] treatiseExConList = record.getTreatiseExCon().split("\\|\\|");
            for (int i = 0; i < uiElCodeList.length; i++) {
                for (int j = 0; j < treatiseExConList.length; j++) {
                    if (i == j){
                        map.put(uiElCodeList[i], treatiseExConList[j]);
                    }
                }
            }



            list.add(map);
        }
        PageModel<Map<String,String>> page=new PageModel<>();
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setRecords(list);
        page.setTotalCount(iPage.getTotal());
        return new ResultModel<PageModel<Map<String,String>>>().success(page);
    }
}
